package com.dudu.access;

import java.util.Calendar;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.dudu.service.ILogService;
import com.dudu.service.impl.LogService;
import com.dudu.utils.DateUtils;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.HttpRequest;

/**
 * 提醒服务
 * 
 * @author gaoxs
 * 
 */
public class RemindUtils {
    // private static String email ="gxsz@yonyou.com";
    // private static String remindPhone ="17001090120";
    // private static String url ="http://172.20.14.212:9280/";
    private static ILogService logService = new LogService();

    /**
     * 提醒服务
     * 
     * @param code
     * @param content
     * @param type
     *            短信：sms 邮件:email 全部:all
     * 
     * @return
     */
    public static String remind(String code, String content, String type) {
	long begin = Calendar.getInstance().getTimeInMillis();
	String email = DictionaryUtils.getValueByTypeAndKey("remind", "remindemails");
	String remindPhone = DictionaryUtils.getValueByTypeAndKey("remind", "remindPhone");
	String url = DictionaryUtils.getValueByTypeAndKey("remind", "url");
	String responsedata = "";
	String result = "-1";
	StringBuffer sb = new StringBuffer();
	sb.append("code=" + code);
	sb.append("&content=" + content);
	sb.append("&type=" + type);
	sb.append("&email=" + email);
	sb.append("&phone=" + remindPhone);
	responsedata = HttpRequest.post(url + "remind/remind.do", sb);
	if (StringUtils.isNotBlank(responsedata)) {
	    JSONObject response = JSONObject.fromObject(responsedata);
	    result = response.optString("result");
	    if (result.equals("0")) {
		result = "0";
	    } else {
		result = "-1";
	    }
	} else {
	    responsedata = "";
	}
	long end = Calendar.getInstance().getTimeInMillis();
	Double time = Double.valueOf((end - begin)) / 1000;
	logService.logIms(time, "remind", "【" + sb.toString() + "】", "【" + responsedata + "】", result, DateUtils.getCurrentTime(), "remind", "");
	return result;
    }

    public static void main(String[] args) {
	RemindUtils.remind("OPEN_DIAL_CHENGQI_test", "test", "all");
    }

}
